package com.jyb.util.capture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.jyb.config.HttpCilentConfig;
import com.jyb.enumeration.ShanghaiDistrict;
import com.jyb.util.UrlUtil;
import com.jyb.vo.CqjhShipVo;
import com.jyb.vo.CqjhVo;

public class CqjhUtil {

	public static List<CqjhShipVo> getAllShips(ShanghaiDistrict district){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		List<CqjhShipVo> cqjhShipVos=new ArrayList<CqjhShipVo>();
		try {
			
			if(ShanghaiDistrict.WY==district){
				
				String url="http://www.spict.com/portinfo/Normal/ShipPlanPage.aspx";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements tables=document.select("table[id=ctl00_ASPxSplitter2_ContentPlaceHolder1_SearchPages_cbVessel_DDD_L_LBT]");
				if(tables.size()>0){
					Elements elements=document.select("table[id=ctl00_ASPxSplitter2_ContentPlaceHolder1_SearchPages_cbVessel_DDD_L_LBT]").get(0).select("tr");
					for (Element element : elements) {
						CqjhShipVo cqjhShipVo=new CqjhShipVo();
						cqjhShipVo.setShanghaiDistrict(ShanghaiDistrict.WY);
						cqjhShipVo.setName(element.select("td").get(0).text());
						cqjhShipVo.setValue(element.select("td").get(0).text());
						cqjhShipVos.add(cqjhShipVo);
					}
				}
				
				
				
			}else if(ShanghaiDistrict.WE==district){
				String url="http://www.sipgzct.com/wat/controllerServlet.do?method=getsubformsinput&querysid=g0001";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("select[name=shipname]").get(0).select("option");
				for (Element element : elements) {
					CqjhShipVo cqjhShipVo=new CqjhShipVo();
					cqjhShipVo.setShanghaiDistrict(ShanghaiDistrict.WE);
					cqjhShipVo.setName(element.text());
					cqjhShipVo.setValue(element.attr("value"));
					cqjhShipVos.add(cqjhShipVo);
				}
				
			}else if(ShanghaiDistrict.WS==district){
				String url="http://www.sect.com.cn/wat/controllerServlet.do?method=getsubformsinput&querysid=g0001";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("select[name=shipname]").get(0).select("option");
				for (Element element : elements) {
					CqjhShipVo cqjhShipVo=new CqjhShipVo();
					cqjhShipVo.setShanghaiDistrict(ShanghaiDistrict.WS);
					cqjhShipVo.setName(element.text());
					cqjhShipVo.setValue(element.attr("value"));
					cqjhShipVos.add(cqjhShipVo);
				}
			}else if(ShanghaiDistrict.WW==district){
				String url="https://www.smct.com.cn/wat/controllerServlet.do?method=getsubformsinput&querysid=g0001";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("select[name=shipname]").get(0).select("option");
				for (Element element : elements) {
					CqjhShipVo cqjhShipVo=new CqjhShipVo();
					cqjhShipVo.setShanghaiDistrict(ShanghaiDistrict.WW);
					cqjhShipVo.setName(element.text());
					cqjhShipVo.setValue(element.attr("value"));
					cqjhShipVos.add(cqjhShipVo);
				}
			}else if(ShanghaiDistrict.YY==district){
				
				String url="http://cx.shsict.com:8100/wat/controllerServlet.do?method=getsubformsinput&querysid=g0001";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("select[name=shipname]").get(0).select("option");
				for (Element element : elements) {
					CqjhShipVo cqjhShipVo=new CqjhShipVo();
					cqjhShipVo.setShanghaiDistrict(ShanghaiDistrict.YY);
					cqjhShipVo.setName(element.text());
					cqjhShipVo.setValue(element.attr("value"));
					cqjhShipVos.add(cqjhShipVo);
				}
			}else if(ShanghaiDistrict.YS==district){
					
				String url="http://www.sgict.com.cn:7001/query/search/comm/com_user.jsp";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("select[name=ename]").get(0).select("option");
				for (Element element : elements) {
					CqjhShipVo cqjhShipVo=new CqjhShipVo();
					cqjhShipVo.setShanghaiDistrict(ShanghaiDistrict.YS);
					cqjhShipVo.setName(element.text());
					cqjhShipVo.setValue(element.text());
					cqjhShipVos.add(cqjhShipVo);
				}
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return cqjhShipVos;
		
	}
	
	public static List<CqjhVo> capture(ShanghaiDistrict district,String value){
		
		List<CqjhVo> list=new ArrayList<CqjhVo>();
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		try {
			
			if(ShanghaiDistrict.WY==district){
				String url="http://www.spict.com/portinfo/Normal/ShipPlanPage.aspx";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				Elements elements=document.select("input");
				
				HttpPost httpPost = new HttpPost("http://www.spict.com/portinfo/Normal/ShipPlanPage.aspx");
				List <NameValuePair> nvps = new ArrayList <NameValuePair>();
				String[] values=value.split("/");
				for (Element element : elements) {
					String name=element.attr("name");
					String val= element.val();
					if("ctl00_ASPxSplitter2_ContentPlaceHolder1_SearchPages_cbVessel_VI".equals(name)){
						nvps.add(new BasicNameValuePair(name,values[3]));
					}else if("ctl00$ASPxSplitter2$ContentPlaceHolder1$SearchPages$cbVessel".equals(name)){
						nvps.add(new BasicNameValuePair(name,value));
					}else if("ctl00$ASPxSplitter2$ContentPlaceHolder1$SearchPages$cbVessel$DDD$L".equals(name)){
						nvps.add(new BasicNameValuePair(name,values[3]));
					}else{
						nvps.add(new BasicNameValuePair(name,val));
					}
					
				}
				httpPost.setEntity(new UrlEncodedFormEntity(nvps));
				CloseableHttpResponse response2 = httpclient.execute(httpPost);
				entity = response2.getEntity();
				str=EntityUtils.toString(entity);
				document=Jsoup.parse(str);
				elements=document.select("table[id=ctl00_ASPxSplitter2_ContentPlaceHolder1_gridView_DXMainTable]").select("tr:gt(1)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					if(elementTds.size()>=10){
						CqjhVo cqjhVo=new CqjhVo();
						cqjhVo.setCm(elementTds.get(0).text());
						cqjhVo.setYwcm(elementTds.get(1).text());
						cqjhVo.setHc("进口航次-"+elementTds.get(2).text()+"/出口航次"+elementTds.get(3).text());
						//cqjhVo.setJck(elementTds.get(3).text());
						cqjhVo.setJhkb(elementTds.get(5).text());
						cqjhVo.setSjkb(elementTds.get(4).text());
						cqjhVo.setJhlb(elementTds.get(6).text());
						cqjhVo.setSjlb(elementTds.get(10).text());
						
						list.add(cqjhVo);
					}
				}
				
				
			}else if(ShanghaiDistrict.WE==district){
				String url="http://www.sipgzct.com/wat/controllerServlet.do?queryid=0001004&shipname="+value+"&querysid=g0001&method=doquery";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("table[id=ctable]").get(0).select("tr:gt(0)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					if(elementTds.size()>=7){
						CqjhVo cqjhVo=new CqjhVo();
						cqjhVo.setCm(elementTds.get(0).text());
						cqjhVo.setYwcm(elementTds.get(1).text());
						cqjhVo.setHc(elementTds.get(2).text());
						cqjhVo.setJck(elementTds.get(3).text());
						cqjhVo.setJhkb(elementTds.get(4).text());
						cqjhVo.setSjkb(elementTds.get(5).text());
						cqjhVo.setJhlb(elementTds.get(6).text());
						cqjhVo.setSjlb(elementTds.get(7).text());
						
						list.add(cqjhVo);
					}
					
				}
				
			}else if(ShanghaiDistrict.WS==district){
				String url="http://www.sect.com.cn/wat/controllerServlet.do?queryid=0001004&shipname="+value+"&querysid=g0001&method=doquery";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("table[id=ctable]").get(0).select("tr:gt(0)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					if(elementTds.size()>=7){
						CqjhVo cqjhVo=new CqjhVo();
						cqjhVo.setCm(elementTds.get(0).text());
						cqjhVo.setYwcm(elementTds.get(1).text());
						cqjhVo.setHc(elementTds.get(2).text());
						cqjhVo.setJck(elementTds.get(3).text());
						cqjhVo.setJhkb(elementTds.get(4).text());
						cqjhVo.setSjkb(elementTds.get(5).text());
						cqjhVo.setJhlb(elementTds.get(6).text());
						cqjhVo.setSjlb(elementTds.get(7).text());
						
						list.add(cqjhVo);
					}
					
				}
				
			}else if(ShanghaiDistrict.WW==district){
				String url="https://www.smct.com.cn/wat//controllerServlet.do?queryid=0001004&shipname="+value+"&querysid=g0001&method=doquery";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("table[id=ctable]").get(0).select("tr:gt(0)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					if(elementTds.size()>7){
						CqjhVo cqjhVo=new CqjhVo();
						cqjhVo.setCm(elementTds.get(0).text());
						cqjhVo.setYwcm(elementTds.get(1).text());
						cqjhVo.setHc(elementTds.get(2).text());
						cqjhVo.setJck(elementTds.get(3).text());
						cqjhVo.setJhkb(elementTds.get(4).text());
						cqjhVo.setSjkb(elementTds.get(5).text());
						cqjhVo.setJhlb(elementTds.get(6).text());
						cqjhVo.setSjlb(elementTds.get(7).text());
						
						list.add(cqjhVo);
					}
				}
				
			}else if(ShanghaiDistrict.YY==district){
				
				String url="http://cx.shsict.com:8100/wat/controllerServlet.do?queryid=0001004&shipname="+value+"&querysid=g0001&method=doquery";
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("table[id=ctable]").get(0).select("tr:gt(0)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					if(elementTds.size()>=7){
						CqjhVo cqjhVo=new CqjhVo();
						cqjhVo.setCm(elementTds.get(0).text());
						cqjhVo.setYwcm(elementTds.get(1).text());
						cqjhVo.setHc(elementTds.get(2).text());
						cqjhVo.setJck(elementTds.get(3).text());
						cqjhVo.setJhkb(elementTds.get(4).text());
						cqjhVo.setSjkb(elementTds.get(5).text());
						cqjhVo.setJhlb(elementTds.get(6).text());
						cqjhVo.setSjlb(elementTds.get(7).text());
						
						list.add(cqjhVo);
					}
				}
				
			}else if(ShanghaiDistrict.YS==district){
					
				String url="http://www.sgict.com.cn:7001/query/search/comm/shipname.jsp?ename="+value;
				url=UrlUtil.hanadleUrl(url);
				
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				Elements elements=document.select("tr[class=result_th]").get(0).parent().select("tr:gt(0)");
				for (Element element : elements) {
					Elements elementTds=element.select("td");
					if(elementTds.size()>=8){
						CqjhVo cqjhVo=new CqjhVo();
						cqjhVo.setCm(elementTds.get(0).text());
						cqjhVo.setYwcm(elementTds.get(1).text());
						cqjhVo.setHc(elementTds.get(2).text());
						cqjhVo.setJck(elementTds.get(3).text());
						cqjhVo.setJhkb(elementTds.get(4).text());
						cqjhVo.setSjkb(elementTds.get(5).text());
						cqjhVo.setJhlb(elementTds.get(6).text());
						cqjhVo.setSjlb(elementTds.get(7).text());
						cqjhVo.setYjg(elementTds.get(8).text());
						
						list.add(cqjhVo);
					}
				}
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(JSON.toJSONString(list));
		return list;
	} 
	
	public static void main(String[] args) {
		//CqjhUtil.capture(ShanghaiDistrict.WE, "AJCD007");
		//CqjhUtil.capture(ShanghaiDistrict.WS, "AJCD002");
		//CqjhUtil.capture(ShanghaiDistrict.WW, "AJCD007");
		//CqjhUtil.capture(ShanghaiDistrict.YY, "ADRIAN MAERSK");
		CqjhUtil.capture(ShanghaiDistrict.YS, "AUGUSTA KONTOR");
	}
}
